package com.white.algebra.utils;

import com.white.algebra.domain.Power;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

public class WebUtils {
    public static <T> T copyParamToBean( Map value , T bean ){
        try {
            BeanUtils.populate(bean, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }

    /**
     * 将字符串转换成为int类型的数据
     */
    public static int parseInt(String strInt,int defaultValue) {
        if(strInt==null||strInt.length()<=0){
            return defaultValue;
        }
        try {
            return Integer.parseInt(strInt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    public static Power powerUtil(List<String> list) throws IllegalArgumentException, IllegalAccessException{
        Power power=new Power();
        Field[] fields=power.getClass().getDeclaredFields();
        for (Field field:fields) {
            for (String s:list) {
                String ss=s.replaceFirst("/","")+"Power";
                if(field.getName().equals(ss)) {
                    field.setAccessible(true);
                    field.set(power,true);
                }
            }
        }
        return power;
    }

}
